package shop

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"fast-boot/common/xerr"
	"github.com/hwUltra/fb-tools/gormx"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改类型
func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(req *types.ShopForm) (resp *types.ShopFormResp, err error) {
	info := model.PmsShopModel{}
	l.svcCtx.GormClient.GormDb.First(&info, req.Id)

	if info.Id == 0 {
		logx.WithContext(l.ctx).Errorf("不存在: %s", req.Id)
		return nil, xerr.NewErrMsg("不存在")
	}
	if len(info.Name) > 0 && info.Name != req.Name {
		info.Name = req.Name
	}

	if len(info.Tel) > 0 && info.Tel != req.Tel {
		info.Tel = req.Tel
	}

	if len(info.Notice) > 0 && info.Notice != req.Notice {
		info.Notice = req.Notice
	}

	if req.Status != -1 {
		info.Status = int8(req.Status)
	}

	if info.DistributionFee != req.DistributionFee {
		info.DistributionFee = req.DistributionFee
	}
	ct := (*model.PmsShopCache)(gormx.NewCacheTool(l.svcCtx.Config.Cache, l.svcCtx.GormClient.GormDb))
	if err := ct.Update(&info); err != nil {
		return nil, err
	}

	return resp, err
}
